package model

import (
	"gorm.io/gorm"
	"time"
	"weibo_1006/global"
)

type Comment struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:id;primaryKey;not null;" json:"id"`  // id
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" json:"user_id"`          // 用户ID
	WeiboId   int32          `gorm:"column:weibo_id;type:int;comment:微博ID;not null;" json:"weibo_id"`        // 微博ID
	Content   string         `gorm:"column:content;type:varchar(300);comment:评论内容;not null;" json:"content"` // 评论内容
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (c *Comment) TableName() string {
	return "comment"
}

// 发布评论
func (c *Comment) AddComment() error {
	return global.DB.Create(*c).Error
}

// 显示评论列表
func (c *Comment) ListComment(key int32) (list []Comment, err error) {
	err = global.DB.Where("weibo_id = ?", key).Find(&list).Error
	return
}

// 删除评论
func (c *Comment) DeleteComment() error {
	return global.DB.Delete(&c).Error
}
